package com.budwk.app.waterquality.services.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.budwk.app.sys.models.Sys_unit;
import com.budwk.app.sys.models.Sys_user;
import com.budwk.app.waterquality.component.CommonComponent;
import com.budwk.app.waterquality.models.WaterSectionInfo;
import com.budwk.app.waterquality.services.WaterSectionInfoService;
import com.budwk.starter.database.service.BaseServiceImpl;
import com.budwk.app.waterquality.models.WaterSectionInfoDetail;
import com.budwk.app.waterquality.services.WaterSectionInfoDetailService;
import com.google.common.collect.Lists;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;

import java.time.LocalDateTime;
import java.util.List;

@IocBean(args = {"refer:dao"})
public class WaterSectionInfoDetailServiceImpl extends BaseServiceImpl<WaterSectionInfoDetail> implements WaterSectionInfoDetailService {
    public WaterSectionInfoDetailServiceImpl(Dao dao) {
        super(dao);
    }

    @Inject
    private WaterSectionInfoService waterSectionInfoService;

    @Override
    public List<WaterSectionInfoDetail> getBySectionId(String sectionId) {
        WaterSectionInfo sectionInfo = waterSectionInfoService.fetch(sectionId);
        if (sectionInfo == null) {
            return Lists.newArrayList();
        }
        List<WaterSectionInfoDetail> list = this.query(Cnd.where(WaterSectionInfoDetail::getSectionId, "=", sectionId));
        if (CollectionUtil.isEmpty(list)) {
            return list;
        }
        list.forEach(e -> e.setSectionInfo(sectionInfo));
        return list;
    }

    @Override
    public void create(WaterSectionInfoDetail water) {
        Sys_user user = CommonComponent.getCurrentUser();
        Sys_unit unit = user.getUnit();
        water.setCreateId(user.getId()).setCreateDate(LocalDateTime.now());
        water.setUpdateId(user.getId()).setUpdateDate(LocalDateTime.now());
        water.setUnitId(unit != null ? unit.getId() : null).setUnitName(unit != null ? unit.getName() : null);
        this.insert(water);
    }
}
